package com.kjj.gift.feign;

import com.kjj.gift.util.HttpUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class OAuth2GetUserOrEmployeeTmpTokenInterceptor implements RequestInterceptor {

    @Value("${oauth2.temp.clientId}")
    private String clientId;
    @Value("${oauth2.temp.clientSecret}")
    private String clientSecret;
    @Override
    public void apply(RequestTemplate requestTemplate) {
        String url = "http://localhost:10120/oauth/token?client_id=%s&client_secret=%s&grant_type=client_credentials";
        url = String.format(url, clientId,clientSecret);

        Map<String, String> result = HttpUtil.sendPost(url);
        String token = result.get("access_token");

        log.error("get token!"+token);
        if (StringUtils.isNotBlank(token))
            requestTemplate.header("Authorization","Bearer "+token);

    }

}
